<template>
    <div>
        <h1>新增员工</h1>
        <table class="table table-bordered">
            <tbody>
                <tr>
                    <td align="right">员工编号</td>
                    <td>
                        <input type="text" v-model="info.UBhao">
                    </td>
                </tr>
                <tr>
                    <td align="right">姓名</td>
                    <td>
                        <input type="text" v-model="info.Uname">
                    </td>
                </tr>
                                <tr>
                    <td align="right">手机号码</td>
                    <td>
                        <input type="text" v-model="info.Uphone">
                    </td>
                </tr>
                                <tr>
                    <td align="right">角色</td>
                    <td>
                        <select v-model="info.Tid">
                            <option value="">请选择</option>
                            <option :value="item.Tid" v-for="item in gettype">{{ item.Tname }}</option>
                        </select>
                    </td>
                </tr>
                <tr>
                    <td align="right">备注</td>
                    <td>
                        <input type="text" v-model="info.Uders">
                    </td>
                </tr>
                                <tr>
                    <td align="right">日期</td>
                    <td>
                        <input type="date" v-model="info.Udate">
                    </td>
                </tr>
                <tr>
                    <td align="right">状态</td>
                    <td>
                        <input type="radio" :value="true" name="state" v-model="info.Ustate">启用
                        <input type="radio" :value="false" name="state" v-model="info.Ustate">禁用
                    </td>
                </tr>
                <tr>
                    <td></td>
                    <td>
                        <input type="button" value="保存" class="btn btn-success" @click="adduse">
                    </td>
                </tr>
            </tbody>
        </table>
    </div>
</template>

<script setup lang="ts">
import {ref,reactive,onMounted} from 'vue'
import axios from 'axios'
import { useRoute,useRouter } from 'vue-router';
import moment from 'moment';
const router = useRouter();
const route = useRoute();

let info:any=reactive({
        "UBhao": moment(new Date()).format("yyyyMMdd")+Math.random().toString().substring(2,5),
        "Uname": "",
        "Tid": "",
        "Uphone": "",
        "Uders": "",
        "Udate": "",
        "Ustate":true,
})
onMounted(()=>{
    GetTypeXl();
})
let gettype:any=ref([]);

const GetTypeXl=()=>{
    axios({
        url:"/api/Use/GetType",
        method:"get"
    })
    .then((res)=>{
        console.log(res);
        gettype.value = res.data;
    })
    .catch((err)=>{
        console.log(err);
    })
}

const adduse=()=>{
    if(info.Uname=="")
{
    alert("姓名不能为空");
    return;
}
    if(info.Uphone=="")
{
    alert("电话不能为空");
    return;
}
    if(info.Tid=="")
{
    alert("角色不能为空");
    return;
}
    if(info.Uders=="")
{
    alert("备注不能为空");
    return;
}
    if(info.Udate=="")
{
    alert("日期不能为空");
    return;
}
    axios({
        url:"/api/Use/AddUse",
        method:"post",
        data:info
    })
    .then((res)=>{
        console.log(res);
        if(res.data>0)
    {
        alert("保存成功");
        router.push("/GetUse");
        return;
    }
    else if(res.data==-1)
    {
        alert("姓名不能重复");
        return;
    }
    else
    {
        alert("保存失败");
        return;
    }
    })
    .catch((err)=>{
        console.log(err);
    })
}
</script>

<style scoped>

</style>